package com.duoec.doc.service.impl.mock.base;

import com.duoec.doc.entity.MockConfigItem;
import com.duoec.doc.service.IBaseTypeMocker;
import com.duoec.doc.service.IMocker;
import com.duoec.doc.helper.MockHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author xuwenzhen
 * @date 2020/2/1
 */
@Service
public class DateBaseTypeMocker implements IMocker, IBaseTypeMocker {

    private static final String DATE_FORMAT = "YYYY-MM-dd HH:mm:ss";

    /**
     * mock配置输出的响应
     *
     * @param item 配置
     * @return
     */
    @Override
    public String mock(MockConfigItem item) {
        if (!MockHelper.available(item)) {
            return null;
        }

        String date = getDate(item);
        if (date == null) {
            return null;
        }
        return "\"" + date + "\"";
    }

    private String getDate(MockConfigItem item) {
        if (item.getMockType().equals(1)) {
            // 当前时间
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
            return sdf.format(new Date());
        }
        if (StringUtils.isEmpty(item.getValue())) {
            return null;
        }
        return item.getValue();
    }

    /**
     * 可处理的基础类型：0=number 1=string 2=boolean 3=byte 4=date 5=enum 6=char
     *
     * @return 类型
     */
    @Override
    public Integer getMockType() {
        return 4;
    }
}
